﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Web.SessionState;

namespace YXShop.Web.filehandle
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class onlinesignup_form : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            string Name = string.Empty;
            int UID = 0;
            if (context.Session["MemberID"] != null)
            {
                Name = context.Session["MemberName"].ToString();
                UID = Convert.ToInt32(context.Session["MemberID"].ToString());
            }
            else
            {
                ChangeHope.WebPage.Script.Alert("您未登陆或注册!");
                ChangeHope.WebPage.Script.GoBack();
            }
            int sid = ChangeHope.WebPage.PageRequest.GetFormInt("hdSendGroupBuyID");
            int productid = ChangeHope.WebPage.PageRequest.GetFormInt("hdProductID");
            if (sid < 0||productid<0)
            {
                ChangeHope.WebPage.Script.Alert("对不起报名失败!");
                ChangeHope.WebPage.Script.GoBack();
            }

            YXShop.BLL.Product.Product_IntegratePurchas bll = new YXShop.BLL.Product.Product_IntegratePurchas();
            YXShop.Model.Product.Product_IntegratePurchas model = bll.GetModelProductID(productid);
            decimal Count = 0;
            decimal Deposit = 0;

            if (model != null)
            {
                Count = decimal.Parse(model.Count.ToString());
                Deposit = decimal.Parse(model.Deposit.ToString());
            }
            YXShop.BLL.Product.GroupBuy_OnlineSignUpInfo osbll = new YXShop.BLL.Product.GroupBuy_OnlineSignUpInfo();
            YXShop.Model.Product.GroupBuy_OnlineSignUpInfo osmodel = new YXShop.Model.Product.GroupBuy_OnlineSignUpInfo();
            if (sp.AllowGroupBuyDeposit)
            {

                YXShop.BLL.Member.MemberAccount mabll = new YXShop.BLL.Member.MemberAccount();
                YXShop.Model.Member.MemberAccount mamoel = mabll.GetModel(UID);
                //查询用户余额
                if (mamoel != null)
                {
                    ChangeHope.DataBase.DataByPage dbp = osbll.GetList(" [order by] id asc", 2, " and sid=" + sid.ToString() + " and uid=" + UID.ToString() + "");
                    if (dbp.DataReader == null)
                    {
                        //查看用户余额是否足够缴纳保证金
                        if (mamoel.Capital >= Deposit)
                        {
                            YXShop.BLL.Member.UserInfoNote noteBll = new YXShop.BLL.Member.UserInfoNote();
                            YXShop.Model.Member.UserInfoNote noteModel = new YXShop.Model.Member.UserInfoNote();
                            noteModel.NoteName = "System";
                            noteModel.NoteType = 1;
                            noteModel.NoteDate = Convert.ToDateTime(System.DateTime.Now.ToShortDateString());
                            noteModel.Causation = "团购在线报名扣除保证金";
                            noteModel.BosomNote = "团购在线报名扣除保证金";
                            noteModel.BuckleOrAdd = 1;
                            noteModel.UserID = UID;
                            noteModel.Username = context.Session["MemberName"].ToString();
                            noteModel.TicketCount = Convert.ToDecimal(Deposit);

                            decimal deductPrice = Convert.ToDecimal(mamoel.Capital - Deposit);
                            if (mabll.Amend(UID, "Capital", deductPrice) != 0)
                            {
                                //记录数据
                                noteBll.Add(noteModel);
                            }
                        }
                        else
                        {
                            ChangeHope.WebPage.Script.Alert("操作失败,帐户余额不足以缴纳保证金！");
                            ChangeHope.WebPage.Script.GoBack();
                        }
                    }
                }
                else
                {
                    ChangeHope.WebPage.Script.Alert("操作失败,用户不存在！");
                    ChangeHope.WebPage.Script.GoBack();
                }
            }
            else
            {
                Deposit = 0;
            }

            
            osmodel.UID = UID;
            osmodel.SID = ChangeHope.WebPage.PageRequest.GetFormInt("hdSendGroupBuyID");
            osmodel.Name = ChangeHope.WebPage.PageRequest.GetFormString("txtName");
            osmodel.QQ = ChangeHope.WebPage.PageRequest.GetFormString("txtQQ");

            osmodel.Deposit = Deposit;

            osmodel.CreateTime = System.DateTime.Now;
            osmodel.Count = Count.ToString();
            osmodel.State = 0;
            osmodel.SignUpCount = ChangeHope.WebPage.PageRequest.GetFormInt("txtSignUp");
            osmodel.Sex = ChangeHope.WebPage.PageRequest.GetFormInt("radSex");
            osmodel.Tel = ChangeHope.WebPage.PageRequest.GetFormString("txtTel");
            osmodel.Address = ChangeHope.WebPage.PageRequest.GetFormString("txtAddress");
            osmodel.Content = ChangeHope.WebPage.PageRequest.GetFormString("txtContent");
            if (osbll.Add(osmodel) != 0)
            {
                ChangeHope.WebPage.Script.Alert("恭喜,报名成功..");
            }
            else
            {
                ChangeHope.WebPage.Script.Alert("很遗憾,您报名失败！");
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
